package com.example.demo.config.scheduler;

import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;

/**
 * Created by Mojo on 2017/9/7.
 * job参数设置
 */
@Component
public class MyScheduler {

	@Autowired
	SchedulerFactoryBean schedulerFactoryBean;
	public void scheduleJobs() throws SchedulerException {
		Scheduler scheduler = schedulerFactoryBean.getScheduler();
		startJob1(scheduler);
//		startJob2(scheduler);
	}

	/**
	 * trigger没有指定任务开始时间，默认为当前时间，也没有指定结束时间，
	 * 结束时间是不确定的，任务会一直执行，只要触发时间满足。
	 * @param scheduler
	 * @throws SchedulerException
	 */
	private void startJob1(Scheduler scheduler) throws SchedulerException{
		JobDetail jobDetail = JobBuilder.newJob(ScheduledJob.class) .withIdentity("job1", "group1").build();
		CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?");
		CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1") .withSchedule(scheduleBuilder).build();
		scheduler.scheduleJob(jobDetail,cronTrigger);
	}
//	private void startJob2(Scheduler scheduler) throws SchedulerException{
//		JobDetail jobDetail = JobBuilder.newJob(ScheduledJob2.class) .withIdentity("job2", "group1").build();
//		CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/10 * * * * ?");
//		CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger2", "group1") .withSchedule(scheduleBuilder).build();
//		scheduler.scheduleJob(jobDetail,cronTrigger);
//	}

}
